package com.haixiaoke.saas.contract.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.contract.domain.ContractTemplateField;
import com.haixiaoke.saas.contract.mapper.ContractTemplateFieldMapper;
import com.haixiaoke.saas.contract.service.IContractTemplateFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 合同模板默认字段Service业务层处理
 *
 * @author panda
 * @date 2024-05-30
 */
@Service
public class ContractTemplateFieldServiceImpl implements IContractTemplateFieldService {
    @Autowired
    private ContractTemplateFieldMapper contractTemplateFieldMapper;

    /**
     * 查询合同模板默认字段
     *
     * @param contractTemplateId 合同模板默认字段主键
     * @return 合同模板默认字段
     */
    @Override
    public ContractTemplateField selectContractTemplateFieldByContractTemplateId(Long contractTemplateId) {
        return contractTemplateFieldMapper.selectContractTemplateFieldByTemplateFieldId(contractTemplateId);
    }

    /**
     * 查询合同模板默认字段列表
     *
     * @param contractTemplateField 合同模板默认字段
     * @return 合同模板默认字段
     */
    @Override
    public List<ContractTemplateField> selectContractTemplateFieldList(ContractTemplateField contractTemplateField) {
        return contractTemplateFieldMapper.selectContractTemplateFieldList(contractTemplateField);
    }

    /**
     * 新增合同模板默认字段
     *
     * @param contractTemplateField 合同模板默认字段
     * @return 结果
     */
    @Override
    public int insertContractTemplateField(ContractTemplateField contractTemplateField) {
        contractTemplateField.setCreateTime(DateUtils.getNowDate());
        return contractTemplateFieldMapper.insertContractTemplateField(contractTemplateField);
    }

    /**
     * 修改合同模板默认字段
     *
     * @param contractTemplateField 合同模板默认字段
     * @return 结果
     */
    @Override
    public int updateContractTemplateField(ContractTemplateField contractTemplateField) {
        contractTemplateField.setUpdateTime(DateUtils.getNowDate());
        return contractTemplateFieldMapper.updateContractTemplateField(contractTemplateField);
    }

    /**
     * 批量删除合同模板默认字段
     *
     * @param templateFieldIds 需要删除的合同模板默认字段主键
     * @return 结果
     */
    @Override
    public int deleteContractTemplateFieldByContractTemplateIds(Long[] templateFieldIds) {
        return contractTemplateFieldMapper.deleteContractTemplateFieldByTemplateFieldIds(templateFieldIds);
    }

    /**
     * 删除合同模板默认字段信息
     *
     * @param templateFieldId 合同模板默认字段主键
     * @return 结果
     */
    @Override
    public int deleteContractTemplateFieldByContractTemplateId(Long templateFieldId) {
        return contractTemplateFieldMapper.deleteContractTemplateFieldByTemplateFieldId(templateFieldId);
    }

    @Override
    public Map<String, Object> selectContractTemplateFieldMap(ContractTemplateField contractTemplateField) {
        List<ContractTemplateField> contractTemplateFieldList = contractTemplateFieldMapper.selectContractTemplateFieldMap(contractTemplateField);
        return contractTemplateFieldList.stream().collect(Collectors.toMap(
                ContractTemplateField::getFieldName,
                x -> StrUtil.isBlank(x.getFieldDefaultValues()) ? "" : x.getFieldDefaultValues()));
    }

    @Override
    public Map<String, Object> selectContractTemplateFieldByNameMap(ContractTemplateField contractTemplateField) {
        List<ContractTemplateField> contractTemplateFieldList = contractTemplateFieldMapper.selectContractTemplateFieldList(contractTemplateField);
        return contractTemplateFieldList.stream().collect(Collectors.toMap(
                ContractTemplateField::getName,
                x -> StrUtil.isBlank(x.getFieldName()) ? "" : x.getFieldName()));
    }

    @Override
    public Map<String, Object> selectContractTemplateFieldByFieldNameMap(ContractTemplateField contractTemplateField) {
        List<ContractTemplateField> contractTemplateFieldList = contractTemplateFieldMapper.selectContractTemplateFieldMap(contractTemplateField);
        return contractTemplateFieldList.stream().collect(Collectors.toMap(
                ContractTemplateField::getFieldName,
                x -> StrUtil.isBlank(x.getName()) ? "" : "{" + x.getName() + "}"));
    }
}
